var config = require('../../config');
var app = getApp();
Page({
  data: {
    post_id: '',
    has_zan: 0,
    has_connect: 0,
    connect_num: 0,
    zan_num: 0,
    page_view: 0,
    info: {
      image_url: '/images/blank.jpg',
    },
  },
  onLoad: function (options) {
    var post_id = options.post_id;
    this.data.post_id = post_id;

    // 先从缓存拉取数据.
    this.initData(post_id);

    // 实时获取文章信息.
    this.getPostInfo(post_id);

    // 获取赞和收藏的数量，以及是否赞和收藏.
    this.getHelper(post_id);

    // 设置浏览次数
    this.setPageView(post_id);
  },
  // 先尝试从缓存拉取数据
  initData: function (post_item_id) {
    var that = this;
    wx.getStorage({
      key: 'post.' + post_item_id,
      success: function (res) {
        if (res.data) {
          that.setData({
            info: res.data,
            page_view: res.data.page_view
          });
          // 设置标题
          wx.setNavigationBarTitle({
            title: res.data.author + '：' + res.data.title
          });
        }
      },
    })
  },
  // 实时从接口拉取数据.
  getPostInfo: function (post_item_id) {
    var that = this;
    wx.request({
      url: config.post_item_url,
      data: {
        post_id: post_item_id
      },
      success: function (res) {
        if (res.statusCode != 200) {
          wx.showToast({
            title: '网络请求错误',
          })
        } else {
          res.data.image_url = 'http://st.imeiwen.org/' + res.data.image_url + '?imageView2/2/w/780/' + res.data.image_up_time;
          that.data.info = res.data;
          that.setData({
            info: res.data,
            page_view: res.data.page_view
          });
          // 刷新缓存
          wx.setStorage({
            key: 'post.' + post_item_id,
            data: res.data
          })
          // 设置标题
          wx.setNavigationBarTitle({
            title: res.data.author + '：' + res.data.title
          });
        }
      }
    });
  },
  getHelper: function (post_item_id) {
    var that = this;
    app.getUserOpenId(function (err, openid) {
      if (!err) {
        wx.request({
          url: config.post_helper_url,
          data: {
            openid: openid,
            post_id: post_item_id
          },
          success: function (res) {
            if (res.statusCode == 200 && res.data) {
              that.data.has_zan = res.data.has_zan;
              that.data.has_connect = res.data.has_zan;
              that.data.zan_num = res.data.zan_num;
              that.data.connect_num = res.data.connect_num;
              that.setData({
                has_zan: res.data.has_zan,
                has_connect: res.data.has_connect,
                zan_num: res.data.zan_num,
                connect_num: res.data.connect_num
              });
            }
          }
        })
      } else {
        wx.request({
          url: config.post_helper_url,
          data: {
            post_id: post_item_id
          },
          success: function (res) {
            if (res.statusCode == 200 && res.data) {
              that.data.zan_num = res.data.zan_num;
              that.data.connect_num = res.data.connect_num;
              that.setData({
                zan_num: res.data.zan_num,
                connect_num: res.data.connect_num
              });
            }
          }
        })
      }
    });
  },
  setPageView: function (post_item_id) {
    var that = this;
    wx.request({
      url: config.post_view_url,
      data: {
        post_id: post_item_id
      },
      success: function (res) {
        if (res.statusCode == 200) {
          that.setData({
            page_view: res.data
          });
        }
      }
    })
  },
  // 收藏操作.
  toConnect: function () {
    var self = this;
    app.getUserOpenId(function (err, openid) {
      if (err) {
        wx.showToast({
          title: '操作失败，未获取到用户信息，请稍后重试',
        })
      } else {
        if (self.data.has_connect > 0) {
          // 取消收藏
          wx.request({
            url: config.post_zc_url,
            data: {
              post_id: self.data.post_id,
              openid: openid,
              op: 'del',
              type: 'connect'
            },
            success: function (res) {
              if (res.statusCode != 200) {
                wx.showToast({
                  title: '网络请求失败',
                })
              } else {
                if (!res.data) {
                  wx.showToast({
                    title: '操作失败',
                  })
                } else {
                  app.globalData.myChanged = true;
                  // 取消收藏成功
                  var connect_num = parseInt(self.data.connect_num) - 1;
                  self.data.has_connect = 0;
                  self.data.connect_num = connect_num;
                  self.setData({
                    has_connect: 0,
                    connect_num: connect_num
                  });
                }
              }
            }
          })
        } else {
          // 添加收藏
          wx.request({
            url: config.post_zc_url,
            data: {
              post_id: self.data.post_id,
              openid: openid,
              op: 'add',
              type: 'connect'
            },
            success: function (res) {
              if (res.statusCode != 200) {
                wx.showToast({
                  title: '网络请求失败',
                })
              } else {
                if (!res.data) {
                  wx.showToast({
                    title: '操作失败',
                  })
                } else {
                  // 收藏成功
                  app.globalData.myChanged = true;
                  var connect_num = parseInt(self.data.connect_num) + 1;
                  self.data.has_connect = 1;
                  self.data.connect_num = connect_num;
                  self.setData({
                    has_connect: 1,
                    connect_num: connect_num
                  });
                }
              }
            }
          })
        }
      }
    });
  },
  // 点赞操作.
  toZan: function () {
    var self = this;
    app.getUserOpenId(function (err, openid) {
      if (err) {
        wx.showToast({
          title: '操作失败，未获取到用户信息，请稍后重试',
        })
      } else {
        if (self.data.has_zan > 0) {
          // 取消点赞
          wx.request({
            url: config.post_zc_url,
            data: {
              post_id: self.data.post_id,
              openid: openid,
              op: 'del',
              type: 'zan'
            },
            success: function (res) {
              if (res.statusCode != 200) {
                wx.showToast({
                  title: '网络请求失败',
                })
              } else {
                if (!res.data) {
                  wx.showToast({
                    title: '操作失败',
                  })
                } else {
                  // 取消点赞成功
                  app.globalData.myChanged = true;
                  var zan_num = parseInt(self.data.zan_num) - 1;
                  self.data.has_zan = 0;
                  self.data.zan_num = zan_num;
                  self.setData({
                    has_zan: 0,
                    zan_num: zan_num
                  });
                }
              }
            }
          })
        } else {
          // 点赞
          wx.request({
            url: config.post_zc_url,
            data: {
              post_id: self.data.post_id,
              openid: openid,
              op: 'add',
              type: 'zan'
            },
            success: function (res) {
              if (res.statusCode != 200) {
                wx.showToast({
                  title: '网络请求失败',
                })
              } else {
                if (!res.data) {
                  wx.showToast({
                    title: '操作失败',
                  })
                } else {
                  // 点赞成功
                  app.globalData.myChanged = true;
                  var zan_num = parseInt(self.data.zan_num) + 1;
                  self.data.has_zan = 1;
                  self.data.zan_num = zan_num;
                  self.setData({
                    has_zan: 1,
                    zan_num: zan_num
                  });
                }
              }
            }
          })
        }
      }
    });
  },
  // 右上角分享
  onShareAppMessage: function () {
    var self = this;
    return {
      title: self.data.info.author + '：' + self.data.info.title,
      path: '/pages/item/item?post_id=' + self.data.info.post_id,
      imageUrl: self.data.info.image_url
    }
  }
})